<template>
  <a-modal
    title="项目类型"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    footer="">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
              label="项目类型名称"
              :labelCol="{lg: {span: 7}, sm: {span:7}}"
              :wrapperCol="{lg: {span: 17}, sm: {span: 17} }">
              <BaseInput
                v-decorator="['typeName',{rules: [{ required: true, message: ' ' }]}]"
                name="name"
                placeholder=""  disabled  />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-item
              label="面积/长度计算"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <a-select disabled v-decorator="['calculateType',{rules: [{ required: true, message: ' ' }]}]">
                <a-select-option value="0">面积计算</a-select-option>
                <a-select-option value="1">长度计算</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :md="12" :sm="12">
            <a-form-item
              label="状态"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span:17}, sm: {span: 17} }">
              <a-select disabled v-decorator="['typeStatus',{rules: [{ required: true, message: ' ' }]}]">
                <a-select-option value="0">不可用</a-select-option>
                <a-select-option value="1">可用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item
              label="主施工范围">
              <a-table
                :row-key="record=>record.id"
                bordered
                :columns="columns" 
                :data-source="data" 
                :pagination="false">
                <span slot="constructionScope" slot-scope="row">
                  <a-input placeholder="请输入" disabled autocomplete="off" v-model="row.scope" />
                </span>
              </a-table>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['typeName', 'calculateType','typeStatus']

export default {
  name:'a'+Date.now(),
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      columns: [{
        title: '主施工范围',
        scopedSlots: { customRender: 'constructionScope' },
        align: 'center',
      }],
      data: [],
    }
  },
  created () {

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if(this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
        this.data = JSON.parse(JSON.stringify(this.model.scopeList));
      }
    })
  }
}
</script>
